package com.njtoyo.taxi.admin.rest.controller.business.ride;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njtoyo.taxi.admin.aop.PermissionScope;
import com.njtoyo.taxi.admin.rest.presenter.business.ride.call.RealTimeSinglePagePresenter;
import com.njtoyo.taxi.admin.rest.presenter.business.ride.call.ReservationPagePresenter;
import com.njtoyo.taxi.admin.rest.wrapper.business.ride.CallWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.ride.CallWrapperDto;
import com.njtoyo.taxi.admin.service.business.ride.CallsService;
import com.taxi.entity.common.RestResult;
import com.njtoyo.taxi.entity.backend.AdminUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Map;

@RestController
@Api(value = "呼叫列表管理", tags = {"呼叫列表管理"})
@RequestMapping("business/call")
public class CallListController {
    @Autowired
    private CallsService callService;

    @GetMapping("/realtime")
    @ApiOperation(value = "实时单列表", notes = "预约单列表,权限:realtime_call_list")
    @PermissionScope(names = {"realtime_call_list"})
    RestResult<Page<RealTimeSinglePagePresenter>> realTimeSingleList(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser,
                                                                     @Valid CallWrapper wrapper) {
        return callService.realTimeSingleList(adminUser, wrapper);
    }

    @GetMapping("/realtimeExport")
    @ApiOperation(value = "实时单列表导出", notes = "实时单列表导出")
    public void realTimeSingleListExport(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser,
                                         @Valid CallWrapperDto wrapper,
                                         HttpServletResponse response) throws IOException {
        callService.getRealTimeSingleListExport(adminUser, wrapper, response);
    }



    @GetMapping("/appointment")
    @ApiOperation(value = "预约单列表", notes = "预约单列表,权限:appointment_call_list")
    @PermissionScope(names = {"appointment_call_list"})
    RestResult<Page<ReservationPagePresenter>> reservationList(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser,
                                                               @Valid CallWrapper wrapper) {
        return callService.reservationList(adminUser, wrapper);
    }

    @GetMapping("/appointmentExport")
    @ApiOperation(value = "预约单导出")
    public void reservationListExport(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser,
                                      @Valid CallWrapperDto wrapper,
                                      HttpServletResponse response) throws IOException{
        callService.getReservationListExport(adminUser, wrapper,response);
    }

    @GetMapping("/assign")
    @ApiOperation(value = "指定单列表", notes = "预约单列表,权限:assign_call_list")
    @PermissionScope(names = {"assign_call_list"})
    RestResult<Page<RealTimeSinglePagePresenter>> specifiedList(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser,
                                                                @Valid CallWrapper wrapper) {
        return callService.specifiedList(adminUser, wrapper);
    }

    @GetMapping("/assignExport")
    @ApiOperation(value = "指定单列表导出")
    public void specifiedListExport(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser,
                                    @Valid CallWrapperDto wrapper,
                                    HttpServletResponse response) throws IOException{
        callService.getSpecifiedListExport(adminUser, wrapper,response);
    }


    @PostMapping("/refund")
    RestResult refund(@RequestBody Map<Object, Object> params) {
        Long id = MapUtil.getLong(params, "id");
        BigDecimal refundAmount = MapUtil.get(params, "refundAmount", BigDecimal.class);
        return callService.refund(id, refundAmount);
    }

}
